package com.atjinggu.jinggumail.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atjinggu.jinggumail.common.utils.R;
import com.atjinggu.jinggumail.product.entity.*;
import com.atjinggu.jinggumail.product.feign.SeckillFeignService;
import com.atjinggu.jinggumail.product.service.*;
import com.atjinggu.jinggumail.product.vo.SeckillSkuInfoVo;
import com.atjinggu.jinggumail.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atjinggu.jinggumail.common.utils.PageUtils;
import com.atjinggu.jinggumail.common.utils.Query;

import com.atjinggu.jinggumail.product.dao.SkuInfoDao;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService imagesService;

    @Autowired
    SpuInfoDescService infoDescService;

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @Autowired
    SeckillFeignService seckillFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        /**
         * key:
         * catelogId: 0
         * brandId: 0
         * min: 0
         * max: 0
         */
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){

            queryWrapper.eq("catalog_id",catelogId);
        }

        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }

        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            queryWrapper.ge("price",min);
        }

        String max = (String) params.get("max");

        if(!StringUtils.isEmpty(max)  ){
            try{
                BigDecimal bigDecimal = new BigDecimal(max);

                if(bigDecimal.compareTo(new BigDecimal("0"))==1){
                    queryWrapper.le("price",max);
                }
            }catch (Exception e){

            }

        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(String spuId) {
        return this.baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }

    @Override
    public SkuItemVo item(String skuId) throws ExecutionException, InterruptedException {
        SkuItemVo vo = new SkuItemVo();
        //sku基本信息pms_sku_info
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity infoEntity = getById(skuId);
            vo.setInfo(infoEntity);
            return infoEntity;
        }, executor);
        //spu 销售属性信息
        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SpuSaleAttrVo> spuSaleAttrVos = skuSaleAttrValueService.selectSaleAttrBySpuId(res.getSpuId());
            vo.setSpuSaleAttrVos(spuSaleAttrVos);
        }, executor);

        //spu介绍信息  pms_spu_info_desc
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
            SpuInfoDescEntity descEntity = infoDescService.getById(res.getSpuId());
            vo.setDesc(descEntity);
        }, executor);

        //spu规格参数信息
        CompletableFuture<Void> attrGroupFuture = infoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SpuAttrGroupInfo> spuAttrGroupInfos = spuInfoService.selectSkuAttrGroupAndAttrBySku(res.getCatalogId(), res.getSpuId());
            vo.setGroupInfo(spuAttrGroupInfos);
        }, executor);

        //spu图片信息
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //sku图片信息 pms_sku_images
            List<SkuImagesEntity> images = imagesService.querySkuImagesBySkuId(skuId);
            vo.setImages(images);
        }, executor);

        //sku秒杀优惠信息
        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            R seckkillInfo = seckillFeignService.getSeckkillInfo(skuId);
            if(seckkillInfo.getCode() == 0){
                SeckillSkuInfoVo data = seckkillInfo.getData(new TypeReference<SeckillSkuInfoVo>() {
                });
                vo.setSeckillSkuInfoVo(data);
            }
        }, executor);


        CompletableFuture.allOf(infoFuture,saleAttrFuture,descFuture,attrGroupFuture,imageFuture,seckillFuture).get();
        return vo;

    }


}